package com.educate.eduorder.controller;


import com.educate.commonutils.ResultOne;
import com.educate.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-30
 */
@RestController
@RequestMapping("/eduorder/pay-log")
@CrossOrigin
public class PayLogController {
    @Autowired
    private PayLogService payLogService;
    //生成微信支付二维码接口
    @GetMapping("createNative/{orderNo}")
    public ResultOne createNative(@PathVariable String orderNo){
        //返回信息,包含二维码地址,还有其他信息
        Map map=payLogService.createNative(orderNo);
        System.out.println("*******返回二维码map集合:"+map);
        return ResultOne.ok().data(map);
    }
    //查询订单状态
    //参数:订单号,根据订单号查询支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public ResultOne queryPayStatus(@PathVariable String orderNo){
         Map<String,String> map=payLogService.queryPayStatus(orderNo);
//        System.out.println("********查询订单状态map:"+map);
         if(map==null){
             return ResultOne.error().message("支付出错了");
         }
         //
        if(map.get("trade_state").equals("SUCCESS")){
            //添加记录到支付表,更新订单的支付状态
            payLogService.updateOrdersStates(map);
            return ResultOne.ok().message("支付成功");
        }
        return ResultOne.error().message("支付失败");

    }

}

